/*************************************************
Author:
Date: 9/25/2012
Description: 在线数据，自身不需要入库
Notes:
*************************************************/

#ifndef __CPlayerOnlineData_h__
#define __CPlayerOnlineData_h__

#include "../maintask/CUserTask.h"
#include "../equip/CUserEquip.h"
#include "../storage/CUserStorage.h"
#include "../partner/CUserPartner.h"
#include "../shop/CUserShop.h"
#include "../instance/CUserInstance.h"
#include "../role/CUserRole.h"
#include "../card/CUserCard.h"
#include "pb_cfg_specialaccount.pb.h"
#include "../energy/CMealDrop.h"
#include "../dailytask/CUserDailyTask.h"
#include "../pet/CUserPet.h"
#include "CTKeyValue.h"

class CPlayerOnlineData
{
public:
    CPlayerOnlineData()
        : _bHashInited(false)
    {
        Clear();

        _uiGID = 0;                     //这4个值在重启服务时需要恢复
        _bIsTencentLogin = true;
        _stTencentApiKeyValue.Clear();
        _ucTencentLoginFrom = TENCENT_LOGIN_FROM_UNKNOWEN;
        _uiNowMount = 0;
        _usNowMountSkillLevel = 0;
        _uiNowWing = 0;
        _ucWingMode = 0;
        _iWingEndTime = 0;
        _uiNowDress = 0;
        _ucDressMode = 0;
        _iDressEndTime = 0;
    }

    virtual ~CPlayerOnlineData()
    {
        CT_ASSERT(false);
    }

    void Clear();

    int Init(const char* sBuff);
    static size_t CountSize();

    int InitData(unsigned int uiGID, CPlayerBaseInfo& stPlayerBaseInfo,
            CPlayerDetailInfo& stPlayerDetailInfo,
            CPlayerDetailInfoEx1 & stPlayerDetailInfoEx1,
            CPlayerSnsDetailInfo& stPlayerSnsDetailInfo);

public:
    bool                        _bIsTencentLogin;       //是否从平台登录
    CTLib::CTKeyValue           _stTencentApiKeyValue;  //腾讯平台通用参数
    CTLib::CTSizeString<80>     _szTencentPayToken;     //腾讯平台支付token
    unsigned char               _ucTencentLoginFrom;    //登录来源

    bool                        _bIsInitFinish;
    int                         _iRoleFighting;         //角色战斗力
    int                         _iTeamFighting;         //队伍战斗力(该值保存阵形中伙伴战斗力总和但不包含主角而前端显示时包含)

    unsigned int                _uiNowMapID;            //当前所在地图
    unsigned short              _usMapPosX;
    unsigned short              _usMapPosY;

    bool                        _bCheckSysOpen;

    unsigned int                _uiGID;
    CUserShop					_stUserShop;
    CUserStorage                _stUserStorage;
    CUserEquip                  _stUserEquip;
    CUserRole                  	_stUserRole;
    CUserStorage                _stUserGroudBag;        //地表上的背包，主要用于副本掉落
    CUserTask                   _stUserTask;
    CUserPartner                _stUserPartner;
    CUserInstance               _stUserInstance;        //副本信息
    CUserCard                   _stUserCard;            //卡片，包括背包和实体
    CMealDrop                   _stMealDrop;            //吃饭(精力系统)
    CUserDailyTask              _stUserDailyTask;       //每日任务
    CUserPet                    _stUserPet;             //宠物
    CAuctionUserData            _stUserAuction;         //个人的拍卖数据.(通知拍卖地图的在线用户, 数据变化时要使用. baseinfo可能被换出)

    //防沉迷信息
    int _iLoginTime;
    int _iDefendValidInfo;      //用户身份验证状态，1：验证通过， 0：未验证通过, 2: 初始化，从未验证过
    int _iDefTotalOnlineTime;   //累计在线时间

    int _iLastSendWorldMsgTime; //最后一次发送世界聊天的时间

    //坐骑相关
    unsigned short _usNowMountSkillLevel;  //当前坐骑的战技等级
    unsigned int _uiNowMount;       //当前的坐骑

    //时装相关
    unsigned char _ucFashionLevel;
    unsigned char _ucWingMode;
    unsigned char _ucDressMode;
    int  _iWingEndTime;
    int  _iDressEndTime;
    unsigned int _uiNowWing;
    unsigned int _uiNowDress;

private:
    bool _bHashInited;
};

class CSpecialAccountMgr
{
public:
    struct SpecialAccountData
    {
        CTSizeString<32> cStrAccountId;
        CTSizeString<32> cStrAccountPasswd;
    };

    int LoadConfig(const char* szLogPath)
    {
        if (szLogPath == NULL)
        {
            CT_ERROR(("Special account Log Path is null"));
            return RET_SYSTEM_FAIL;
        }

        pb_cfg_specialaccount stSpecialAccountPBConfig;
        std::fstream stFileInput(szLogPath, std::ios::in | std::ios::binary);
        if (!stSpecialAccountPBConfig.ParseFromIstream(&stFileInput))
        {
            CT_ERROR(("Failed to Parse SpecialAccount!"));
            return RET_SYSTEM_FAIL;
        }

        //解析配置文件内容
        CT_TRACE(("special account config count %d", stSpecialAccountPBConfig.special_account_rows_size()));
        for (int i = 0; i < stSpecialAccountPBConfig.special_account_rows_size(); ++i)
        {
            const pb_cfg_specialaccount_pb_cfg_special_account_unit& stSaPBConfigUnit
                = stSpecialAccountPBConfig.special_account_rows(i);

            if( "0"==stSaPBConfigUnit.account_id() )
            {
                m_cStrInnerPasswd.Set( "%s", stSaPBConfigUnit.account_passwd().c_str() );
            }
            else
            {
                SpecialAccountData sad;
                sad.cStrAccountId.Set( "%s", stSaPBConfigUnit.account_id().c_str() );
                sad.cStrAccountPasswd.Set( "%s", stSaPBConfigUnit.account_passwd().c_str() );

                m_stSpecialAccountArray.AddOneItem( sad );
            }
        }
        return 0;
    }

    /*
    return value: 0,successful
    -1: not valid user
    */
    int CheckAccount(const char * szAccountId, const char * szPasswd)
    {
        if( CheckInnerAccount(szPasswd) )
        {
            CT_TRACE(("special account:inner passwd %s", szAccountId));
            return 0;
        }

        for( int i=0; i<m_stSpecialAccountArray.Size(); i++ )
        {
            if( m_stSpecialAccountArray[i].cStrAccountId == szAccountId &&
                m_stSpecialAccountArray[i].cStrAccountPasswd == szPasswd )
            {
                CT_TRACE(("special account:id:%s, passwd:%s", szAccountId, szPasswd));
                return 0;
            }
        }
        return -1;
    }

protected:
    bool CheckInnerAccount(const char *szPasswd)
    {
        bool bRet = false;
        if( m_cStrInnerPasswd==szPasswd )
        {
            bRet = true;
        }
        return bRet;
    }

private:
    CTArray<SpecialAccountData, MAX_SPECIAL_ACCOUNT>   m_stSpecialAccountArray;        //版署账号
    CTSizeString<32> m_cStrInnerPasswd;                                                //内部人账号的密码
};

#endif // __CPlayerOnlineData_h__
